const Router = require('koa-router')
const callCloudFn = require('../../../core/callCloudFn')
const callCloudDB = require('../../../core/callCloudDB.js')
const cloudStorage = require('../../../core/callCloudStorage.js')
const {
    periodicalAddValidator
} = require('../../validators/validator')
const {
    Auth
} = require('../../../middlewares/auth')

const router = new Router({
    prefix: '/v1/osjj'
})
// get post

router.post('/list', async (ctx, next) => { //列表
    const params = ctx.request.body
    const query = `
        db.collection('cp').skip(${params.start}).limit(${params.count}).get()
    `
    const res = await callCloudDB(ctx, 'databasequery', query)
    let data = res.data.map(item => JSON.parse(item))

    // 图片
    let fileListImg = data.map(item => {
        return {
            fileid: item.img,
            max_age: 7200
        }
    })
    const dlResImg = await cloudStorage.download(ctx, fileListImg)
    // 
    data.forEach(item => {
        dlResImg.file_list.forEach(op => {
            if (item.img == op.fileid) {
                item.img_url = op.download_url
            }
        })
    })
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data:{
            list:data,
            total:100
        }
    }
})

router.post('/add', async (ctx, next) => { //新增
    const params = ctx.request.body
    const query = `
        db.collection('cp').add({
            data: {
                name: '${params.name}',
                type: '${params.type}',
                price: '${params.price}',
                txt: '${params.txt}'
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseadd', query)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data:res
    }
})
router.post('/up', async (ctx, next) => { // 更新
    const params = ctx.request.body
    const query = `
        db.collection('cp').doc('${params._id}').update({
            data: {
                name: '${params.name}',
                type: '${params.type}',
                price: '${params.price}',
                txt: '${params.txt}'
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data:res
    }
})
router.post('/updateImg', async (ctx, next) => {// 上传图片
    const fileid = await cloudStorage.upload(ctx)
    const params = ctx.request.body
    const query = `
        db.collection('cp').doc('${params._id}').update({
            data: {
                img: '${fileid}',
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data: res
    }
})
router.post('/del', async (ctx, next) => {// 删除
    // 删数据
    const params = ctx.request.body
    const query = `db.collection('cp').doc('${params._id}').remove()`
    const res = await callCloudDB(ctx, 'databasedelete', query)
    //删图片
    const delStorageRes = await cloudStorage.delete(ctx, params.img)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data: res
    }
})

module.exports = router